from BaseAcController import AcController


class MediaAC(AcController):
    def __init__(self):
        super().__init__()
        base_tick = 80  # 80微秒，   Midea基础时间单位，所有时间参数都是它的整数倍

        self.head_mark = base_tick * 56  # 起始信号高电平时长
        self.head_space = base_tick * 56  # 起始信号低电平时长
        self.bit_mark = base_tick * 7  # 数据位高电平时长
        self.one_space = base_tick * 21  # 数据位1低电平时长
        self.zero_space = base_tick * 7  # 数据位0低电平时长
        self.footer_space = self.head_mark + self.one_space + self.zero_space  # 消息结束间隔

        self.remote_state = 0xA1826FFFFF62  # 默认状态

        self.media24_base_space = 13000  # Midea24协议特有的消息结束间隔
        self.init_ir()

    def test_open(self):
        # 发送两次（正常数据+反转数据）
        for phase in [False, True]:
            # 发送引导码
            self.ir_transmitter.send_segment(self.head_mark)
            self.ir_transmitter.send_segment(self.head_space)

            ac_state = 0xA1826FFFFF62
            if phase:
                ac_state = ~ac_state  # 第二相位发送反转数据
            bits = ""
            # 从最高位（第47位）到最低位（第0位）依次提取
            for i in range(47, -1, -1):
                bits += str((ac_state >> i) & 0x01)

            for bit in bits:
                self.ir_transmitter.send_bit(bit)

            # 发送结束信号
            self.ir_transmitter.send_segment(self.bit_mark)
            self.ir_transmitter.send_segment(self.footer_space)
